package com.san.metadata.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidPooledPreparedStatement;
import com.san.base.dao.BaseManager;
import com.san.common.utils.DateUtils;
import com.san.common.utils.StringUtils;
import com.san.metadata.domain.DataSourceDO;
import com.san.metadata.domain.TechnicalTableDO;
import com.san.metadata.service.DataSourceService;

/**
 * 数据导入JDBC访问层
 * 
 * @author xiangyz
 *
 */
@Service
public class DataImportManager extends BaseManager {

	@Autowired
	DataSourceService dataSourceService;

	/**
	 * 批量保存技术元数据表
	 * 
	 * @param addBaseList
	 * @return
	 */
	public List<List<String>> batchSave(TechnicalTableDO technicalTableDO, Map<String,Object> result, List<List<String>> list) throws Exception {
		
		String columns = (String) result.get("columns");
		Integer columnSize = (Integer) result.get("columnSize");
		List<Integer> dateList = (List<Integer>) result.get("dateColumnList");
		int autoInputDateIndex = (Integer)result.get("autoInputDateIndex");
		
		LinkedList<List<String>> errorList = new LinkedList<List<String>>();
		if (StringUtils.isEmpty(columns)) {
			return errorList;
		}
		DataSourceDO dataSourceDO = (DataSourceDO) dataSourceService.getById(technicalTableDO.getTargetSourceId());
		try {
			StringBuffer insertSQL = new StringBuffer("insert into ");
			insertSQL.append(technicalTableDO.getTargetTableName());
			insertSQL.append(" (").append(columns).append(" ) values (");
			for (int i = 0; i < columnSize; i++) {
				insertSQL.append("?");
				if (i < columnSize - 1) {
					insertSQL.append(",");
				}
			}
			insertSQL.append(")");
			Connection conn = this.getDBConnection(dataSourceDO.getSourceCode());
			DruidPooledPreparedStatement ps = null;

			try {
				ps = (DruidPooledPreparedStatement) conn.prepareStatement(insertSQL.toString());
				// 设置为手动提交
				conn.setAutoCommit(false);
				// 从第二行开始，标题无需导入
				for (int i = 1; i < list.size(); i++) {
					List<String> values = list.get(i);
					try {
						int index = 1;
						for (int j = 0; j < values.size(); j++) {
							if (j == autoInputDateIndex) {
								ps.setObject(index, new Date());
								index++;
							}
							if (null != dateList && dateList.contains(j)) {
								ps.setObject(index, DateUtils.stringToDate(values.get(j)));
							}
							ps.setObject(index, values.get(j));
							index++;
						}
						ps.addBatch();
					} catch (Exception e) {
						errorList.add(values);
					}
				}

				ps.executeBatch();
				// 提交，批量插入数据库中。
				conn.commit();
			} catch (SQLException e) {
				throw e;
			} finally {
				if (null != ps) {
					ps.close();
				}
				if (null != conn) {
					conn.close();
				}
			}
		} catch (Exception e) {
			throw e;
		}
		if (errorList.size() > 0) {
			errorList.addFirst(list.get(0));
		}

		return errorList;
	}
}
